#include "task_mqtt_comm.h"

#include "FreeRTOS.h"
#include "task.h"

#include "device_resource.h"
#include "hc165_quad.h"
#include "mqttclient.h"
#include "print_rtt.h"
#include "rtl_private_op.h"

#include "w6100_tcp_comm_op.h"

static StackType_t xMQTTCommStack[4096]; // __attribute__((at(0x10000000), aligned(8)));
// 静态栈
static StaticTask_t xMQTTCommTCB; // 静态 TCB（可选，省堆）

//uint8_t buffer[2048];

static void vMQTTCommTaskEntry(void *pvParameters)
{
  (void)pvParameters;
  mqtt_gen_sub_topic_group();
  for (;;)
  {
    DEBUG_RTT_READ();
    switch (w6100_net_status)
    {
    case W6100_READY:
		case W6100_DHCP_FAIL:
      mqtt_client_process();
      break;
    default:
			break;
      /* code */
    }

    vTaskDelay(pdMS_TO_TICKS(100));
  }
}
void vMQTTCommTaskCreate(void)
{
  TaskHandle_t xHandle = NULL;
  taskENTER_CRITICAL(); // 进入临界区
  /* 创建system_led任务 - 静态方法 */
  xHandle = xTaskCreateStatic(
      (TaskFunction_t)vMQTTCommTaskEntry,                     /* 任务入口函数 */
      (const char *)"MQTT_comm",                              /* 任务名字 */
      (uint16_t)sizeof(xMQTTCommStack) / sizeof(StackType_t), /* 任务栈大小 */
      (void *)NULL,                                           /* 任务入口函数参数 */
      (UBaseType_t)5,                                         /* 任务的优先级 */
      (StackType_t *)xMQTTCommStack,                          /* 静态栈数组 */
      (StaticTask_t *)&xMQTTCommTCB);                         /* 静态TCB的地址 */

  if (xHandle != NULL)
  {
    LOG_INFO("Create mqtt comm Task success!\r\n");
  }
  taskEXIT_CRITICAL(); // 退出临界区，无论成功失败都要退出
}
